package main

import (
	"go.uber.org/zap"
	"mts/trans_srv/internal/repository"
	"mts/trans_srv/internal/repository/dao"
	"mts/trans_srv/internal/service"
	"mts/trans_srv/internal/web"
	"mts/trans_srv/ioc"
)

func failOnError(err error, msg string) {
	if err != nil {
		zap.S().Fatalf("%s: %s", msg, err)
	}
}
func main() {
	ioc.InitLogger()
	ioc.InitConfig()
	db := ioc.InitDB()
	if err := ioc.InitTrans("zh"); err != nil {
		panic(err)
	}
	rmq, err := ioc.InitRabbiMQ()
	failOnError(err, "Failed to initialize RabbitMQ")
	defer rmq.Close()
	//dao
	languageDao := dao.NewLanguageDao(db)
	translationDao := dao.NewTranslationDao(db)
	elementDao := dao.NewElementDao(db)
	//repo
	languageRepository := repository.NewLanguageRepository(languageDao)
	translationRepository := repository.NewTranslationRepository(translationDao)
	elementRepository := repository.NewElementRepository(elementDao)
	//svc
	langService := service.NewLanguageService(languageRepository, rmq)
	translationService := service.NewTranslationService(translationRepository, languageRepository, elementRepository)
	elementService := service.NewElementService(languageRepository, elementRepository, translationRepository, rmq)
	//web
	langHandler := web.NewLanguageHandler(langService)
	transHandler := web.NewTranslationHandler(translationService, langService)
	elementHandler := web.NewElementHandler(elementService)
	srv := ioc.InitWebServer(langHandler, transHandler, elementHandler)
	err = srv.Run(":8022")
	if err != nil {
		zap.S().Fatalf("服务启动失败:%s", err.Error())
	}
}
